<template>
  <div class="box">
    <el-card style="width: 800px; margin-top: 180px; height: 300px">
      <el-form
        ref="ruleForm"
        style="margin-top: 40px"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="ruleForm.name" />
        </el-form-item>
        <!-- <el-form-item label="菜单路由" prop="path">
          <el-input v-model="ruleForm.path" />
        </el-form-item> -->
        <el-form-item style="margin-top: 100px">
          <el-button
            type="primary"
            @click="submitForm('ruleForm')"
          >提交</el-button>
          <el-button @click="resetForm('ruleForm')">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<script>
import { add } from './api/index'
import { getMenuObj, removeMenuObj } from '@/utils/auth' // get token from cookie

export default {
  data() {
    return {
      ruleForm: {
        name: '',
        path: ''
      },
      menuId: '',
      rules: {
        name: [
          { required: true, message: '请输入活菜单名称', trigger: 'blur' }
        ],
        path: [{ required: true, message: '请输入活菜单路由', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.menuId = getMenuObj().id
    console.log(getMenuObj())
    if (getMenuObj().children.length > 0) {
      this.ruleForm.path = `${getMenuObj().idPath}${
        getMenuObj().children[getMenuObj().children.length - 1].id
      }`
    } else {
      this.ruleForm.path = `${getMenuObj().idPath}0`
    }
  },
  mounted() {
    console.log(this.$route) // undefined
  },
  methods: {
    submitForm(formName) {
      this.$router.push({
        path: '/dashboard'
      })
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const config = Object.assign({}, this.ruleForm, {
            menuId: this.menuId
          })
          console.log(config)
          add(config).then((res) => {
            console.log(res)
            if (res.code === 1) {
              this.$message.success('新增成功')
              // this.$store.dispatch('tagsView/delView', this.$route)
              this.$router.push({
                path: '/dashboard'
              })
              setTimeout(() => {
                location.reload()
              }, 1000)
            } else {
              console.log(res)
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      removeMenuObj()
      // this.$store.dispatch('tagsView/delView', this.$route)
      // this.$router.push({
      //   name: 'dashboard'
      // })
      this.$router.go(-1)
    }
  }
}
</script>
<style scoped>
.box {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
</style>

